package Stock.StockGUIS;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import com.toedter.calendar.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import Stock.StockDAOS.*;

public class StockEditPanel extends JPanel implements ActionListener
{
	private JPanel southP=null;
	JButton editB=null, delB=null, saveB=null, returnB=null;
	StockDetailsPanel sdp;
	StockNew stock;
	StockNewManager snm;
	StockNew sn;

	public StockEditPanel(StockDetailsPanel sdp, Connection Conn) 
	{
		this.sdp = sdp;
		southP = new JPanel(); 
		BoxLayout b4 = new BoxLayout(southP, BoxLayout.X_AXIS);
		southP.setLayout(b4);
		editB = new JButton(" Edit Stock Item");
		editB.addActionListener(this);
		returnB = new JButton(" Back ");
		returnB.addActionListener(this);
		delB = new JButton(" Delete Stock Item");
		delB.addActionListener(this);
		saveB = new JButton(" Save Changes ");
		saveB.addActionListener(this);
		
		southP.add(editB);
		southP.add(delB);
			
		southP.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		snm = new StockNewManager(Conn);
		
		this.add(southP);
	}	
	
	public StockEditPanel(StockDetailsPanel sdp, StockNew sn, Connection Conn)
	{
		this.sdp = sdp;
		this.sn = sn;
		
		southP = new JPanel(); 
		BoxLayout b4 = new BoxLayout(southP, BoxLayout.X_AXIS);
		southP.setLayout(b4);
		editB = new JButton(" Edit Stock Item");
		editB.addActionListener(this);
		returnB = new JButton(" Back ");
		delB = new JButton(" Delete Stock Item");
		delB.addActionListener(this);
		saveB = new JButton(" Save Changes ");
		saveB.addActionListener(this);
		
		southP.add(editB);
		southP.add(delB);
			
		southP.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
		snm = new StockNewManager(Conn);
		
		this.add(southP);
	}
	
	public void emptyAll()
	{
		sdp.mediaStockIdTxt.setText("");
		sdp.stockTypeJcb.setSelectedItem("");
		sdp.barcodeTxt.setText("");
		sdp.brandTxt.setText("");
		sdp.serialNoTxt.setText("");
		sdp.descriptionTxt.setText("");
		sdp.titleTxt.setText("");
		sdp.catIdJcb.setSelectedItem("");
		sdp.runTimeTxt.setText("");
		sdp.ageRestTxt.setText("");
		sdp.directorTxt.setText("");
		sdp.actor1Txt.setText("");
		sdp.actor2Txt.setText("");
		sdp.releaseDateTxt.setText("");
		sdp.genreJcb.setSelectedItem("");
		sdp.statusJcb.setSelectedItem("");
		sdp.lastJdc.setDate(null);	
	}
	
	public void setFieldsSelectable(Boolean tf)
	{
		if(tf)
		{
			sdp.stockTypeJcb.setEnabled(true);
			sdp.barcodeTxt.setEnabled(true);
			sdp.brandTxt.setEnabled(true);
			sdp.serialNoTxt.setEnabled(true);
			sdp.descriptionTxt.setEnabled(true);
			sdp.titleTxt.setEnabled(true);
			sdp.catIdJcb.setEnabled(true);
			sdp.runTimeTxt.setEnabled(true);
			sdp.ageRestTxt.setEnabled(true);
			sdp.directorTxt.setEnabled(true);
			sdp.actor1Txt.setEnabled(true);
			sdp.actor2Txt.setEnabled(true);
			sdp.lastJdc.setEnabled(true);
			sdp.releaseDateTxt.setEnabled(true);
			sdp.genreJcb.setEnabled(true);
			sdp.statusJcb.setEnabled(true);
		}
		else if(tf ==  false)
		{
			sdp.mediaStockIdTxt.setEnabled(false);
			sdp.stockTypeJcb.setEnabled(false);
			sdp.barcodeTxt.setEnabled(false);
			sdp.brandTxt.setEnabled(false);
			sdp.serialNoTxt.setEnabled(false);
			sdp.descriptionTxt.setEnabled(false);
			sdp.titleTxt.setEnabled(false);
			sdp.catIdJcb.setEnabled(false);
			sdp.runTimeTxt.setEnabled(false);
			sdp.ageRestTxt.setEnabled(false);
			sdp.directorTxt.setEnabled(false);
			sdp.actor1Txt.setEnabled(false);
			sdp.actor2Txt.setEnabled(false);
			sdp.lastJdc.setEnabled(false);
			sdp.releaseDateTxt.setEnabled(false);
			sdp.genreJcb.setEnabled(false);
			sdp.statusJcb.setEnabled(false);
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == returnB)
		{
			southP.removeAll();
			southP.add(editB);
			southP.add(delB);
			delB.setEnabled(true);
			sdp.stockTypeJcb.requestFocus();
			this.repaint();	
			this.validate();
			
		}
		if(source == editB)
		{
			if((sdp.stockTypeJcb.getSelectedItem().equals(""))||(sdp.titleTxt.getText().equals(""))||(sdp.catIdJcb.getSelectedItem().equals(""))||(sdp.statusJcb.getSelectedItem().equals("")))
			{
				JOptionPane.showMessageDialog(new JFrame(), "Select a stock item to be edited!");
				return;
			}
			else
			{
				setFieldsSelectable(true);
				delB.setEnabled(false);
				sdp.validate();
				southP.removeAll();
				southP.add(saveB);
				southP.add(returnB);
				southP.add(delB);
				sdp.stockTypeJcb.requestFocus();
				this.repaint();
				this.validate();
					
			}
				
		}
		if(source == delB)
		{
			if(sn == null)
			{
				
				JOptionPane.showMessageDialog(new JFrame(), "Select a stock item to be deleted!");
				return;
			}
			
		}
		if(source == saveB)
		{
			java.util.Date date = sdp.lastJdc.getDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String bdate = sdf.format(date);
			
			String rdate = sdp.releaseDateTxt.getText();
			
			String [] attr = new String [17];
			attr[0] = sdp.mediaStockIdTxt.getText();
			attr[1] = (String)sdp.stockTypeJcb.getSelectedItem();
			attr[2] = sdp.titleTxt.getText();
			attr[3] = (String)sdp.statusJcb.getSelectedItem();
			attr[4] = sdp.barcodeTxt.getText();
			attr[5] = sdp.brandTxt.getText();
			attr[6] = sdp.serialNoTxt.getText();
			attr[7] = sdp.descriptionTxt.getText();
			attr[8] = (String)sdp.catIdJcb.getSelectedItem();
			attr[9] = sdp.runTimeTxt.getText();
			attr[10] = rdate;
			attr[11] = (String)sdp.genreJcb.getSelectedItem();
			attr[12] = sdp.ageRestTxt.getText();
			attr[13] = sdp.directorTxt.getText();
			attr[14] = sdp.actor1Txt.getText();
			attr[15] = sdp.actor2Txt.getText();
			attr[16] = bdate;
			
			stock = new StockNew(attr);
			snm.update(stock);
			southP.remove(saveB);
			southP.remove(returnB);
			southP.remove(delB);
			southP.add(editB);
			southP.add(delB);
			delB.setEnabled(true);
			this.repaint();
			this.validate();
			
			JOptionPane.showMessageDialog(this, "Changes Saved!");
			emptyAll();
		}
	}
}
